package com.cwvs.cat;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import com.cwvs.cat.adapter.ClubDynamicListAdapter;
import com.cwvs.cat.bean.ClubTrends;
import com.cwvs.cat.myview.BaseActivity;
import com.cwvs.cat.myview.GifView;
import com.cwvs.cat.myview.XListView;
import com.cwvs.cat.myview.XListView.IXListViewListener;
import com.cwvs.cat.net.Port;

public class TabActivityOfClub extends BaseActivity implements IXListViewListener{

	private static XListView lv_fg_club_list;
	private static ClubDynamicListAdapter clubDynamicListAdapter;
	private static List<ClubTrends> list_club_dynamic;
	private static Context context;
	private static Activity activity ;
	private static GifView loading; 
	private static int page = 1;
	private static List<ClubTrends> lTrends;
	private static boolean flag = false;
	private static int pos = 0; 
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_club);
		setHeaderGone();
		initView();
		activity = TabActivityOfClub.this;
		context = TabActivityOfClub.this;
		list_club_dynamic = new ArrayList<ClubTrends>();
		lTrends = new ArrayList<ClubTrends>();
		loading.setVisibility(View.VISIBLE);
		orgList(page,false);
	}
	private void initView() {
		lv_fg_club_list = (XListView) findViewById(R.id.lv_fg_club_list);
		lv_fg_club_list.setPullLoadEnable(true);
		lv_fg_club_list.setXListViewListener(this);
		loading = (GifView) findViewById(R.id.loading);  
		loading.setMovieResource(R.drawable.loading1);  
		lv_fg_club_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				intent.setClass(TabActivityOfClub.this,ClubDynamicReplyActivity.class);
				intent.putExtra("clubtrends",list_club_dynamic.get(arg2-1).id);
				startActivity(intent);
				
			}
		});
	}
	
	
	private static void orgList(final int page,final boolean boo){
		AjaxParams params = new AjaxParams();
		params.put("token",Applications.user.token);
		params.put("page",page+"");
		params.put("pages","10");
		System.out.println("params==="+params.toString());
		FinalHttp fh = new FinalHttp();
		fh.post(Port.orgList,params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
				lv_fg_club_list.stopLoadMore();
				lv_fg_club_list.stopRefresh();
				loading.setVisibility(View.GONE);
			}
			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				loading.setVisibility(View.GONE);
				System.out.println(t.toString());
				JSONObject json;
				try {
					JSONObject obj = null;
					JSONArray array=null,array2=null,arr = null;
					json = new JSONObject(t.toString());
					int code = json.getInt("code");
					if (code==0) {
						obj=json.getJSONObject("list");
						array2=obj.getJSONArray("header");
						for (int i = 0; i < array2.length(); i++) {
							String cate2 = (String) array2.get(i);
							//list2.add(cate2 );
						}
						array=obj.getJSONArray("data");
						JSONArray array3 = array.getJSONArray(0);
						JSONObject jsonObject = array3.getJSONObject(0);
						JSONObject jsonObject2 = jsonObject.getJSONObject("list");
						JSONArray array1 =jsonObject2.getJSONArray("data");
						for (int i = 0; i < array1.length(); i++) {
							arr = (JSONArray) array1.get(i);
							List<String>	list1 = new ArrayList<String>();
							for (int k = 0; k < arr.length(); k++) {
								String cate1 =arr.get(k).toString();
								list1.add(cate1);
							}
							ClubTrends friendTrends = new ClubTrends(list1);
							if (page==1) {
								list_club_dynamic.add(friendTrends);
							}else {
								lTrends.add(friendTrends);
								for (int j = 0; j < lTrends.size(); j++) {
									list_club_dynamic.add(lTrends.get(j));
								}
							}
						}
						if (page==1) {
							clubDynamicListAdapter = new ClubDynamicListAdapter(activity,list_club_dynamic);
							lv_fg_club_list.setAdapter(clubDynamicListAdapter);
						}else {
							clubDynamicListAdapter.notifyDataSetChanged();
						}
						lv_fg_club_list.stopLoadMore();
						lv_fg_club_list.stopRefresh();
					}else {
						lv_fg_club_list.stopLoadMore();
						lv_fg_club_list.stopRefresh();
						Toast.makeText(activity,json.getString("msg"),2000).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page = 1;
		list_club_dynamic.clear();
		orgList(page,false);
		
		
	}
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		lTrends.clear();
		++page;
		orgList(page,false);
		
	}
	public static void newsGoods(String news_id){
		AjaxParams params = new AjaxParams();
		params.put("token",Applications.user.token);
		params.put("news_id",news_id);
		System.out.println("params==="+params.toString());
		FinalHttp fh = new FinalHttp();
		fh.post(Port.newsGoods,params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
			}
			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
			//	System.out.println(t.toString());
				JSONObject json;
				try {
					json = new JSONObject(t.toString());
					int code = json.getInt("code");
					if (code==0) {
						clubDynamicListAdapter.notifyDataSetChanged();
					}else {
						Toast.makeText(context,json.getString("msg"),2000).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	public static boolean zan(int positoin){
		pos = positoin;
		AjaxParams params = new AjaxParams();
		params.put("token",Applications.user.token);
		params.put("news_id",list_club_dynamic.get(positoin).id);
		System.out.println("params==="+params.toString());
		FinalHttp fh = new FinalHttp();
		fh.post(Port.newsGoods,params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
			}
			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				System.out.println(t.toString());
				JSONObject json;
				try {
					json = new JSONObject(t.toString());
					int code = json.getInt("code");
					if (code==0) {
						flag = true;
						list_club_dynamic.get(pos).zan = !list_club_dynamic.get(pos).zan;
						if (list_club_dynamic.get(pos).zan) {
							int a =Integer.parseInt(list_club_dynamic.get(pos).goods_count)+1;
							list_club_dynamic.get(pos).goods_count = a+"";
						}else {
							int a =Integer.parseInt(list_club_dynamic.get(pos).goods_count)-1;
							list_club_dynamic.get(pos).goods_count = a+"";
						}
						clubDynamicListAdapter.notifyDataSetChanged();
					}else {
						Toast.makeText(context,json.getString("msg"),2000).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		return flag;
	}
}
